package com.jiusit.golf.api;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.json.JSONObject;

import com.webbuilder.common.Var;
import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.FileUtil;
import com.webbuilder.utils.StringUtil;
import com.webbuilder.utils.SysUtil;
import com.webbuilder.utils.WebUtil;

public class ImageUpload extends HttpServlet  {

	
	 @Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPost(req, resp);
		
		try {
			 String id=SysUtil.getUUID();
			Map<String, Object> map = new HashMap<String, Object>();
			List<Map<String, Object>> photos = new ArrayList<Map<String, Object>>();
			String tempPath=Var.get("api.temppath")+"/temp/";
			 String path=Var.get("api.temppath")+"/"+id+"/"+DateUtil.formatDate(new Date(), "yyyyMMddHH")+"/";
			 String url=path;
			 url=StringUtil.replace(url,"//","/");
			 url=url.substring(url.indexOf("/upload/")+1,url.length());
			 FileUtil.directoryCreate(path);
			 FileUtil.directoryCreate(tempPath);
	           // Create a factory for disk-based file items
	           DiskFileItemFactory factory = new DiskFileItemFactory();
	 
	           // Set factory constraints
	           factory.setSizeThreshold(4096); // 设置缓冲区大小，这里是4kb
	           
	           factory.setRepository(new File(tempPath));// 设置缓冲区目录
	 
	           // Create a new file upload handler
	           ServletFileUpload upload = new ServletFileUpload(factory);
	 
	           // Set overall request size constraint
	           upload.setSizeMax(419430400); // 设置最大文件尺寸，这里是4MB
	 
	           List<FileItem> items = upload.parseRequest(req);// 得到所有的文件
	           Iterator<FileItem> i = items.iterator();
	           while (i.hasNext()) {
	              FileItem fi = (FileItem) i.next();
	              String fileName = fi.getName();
	              if (fileName != null) {
	                  File fullFile = new File(fi.getName());
	                  
	                  String fileExt=FileUtil.extractFileExt(fullFile.getName());
	                  String file_name=DateUtil.formatDate(new java.util.Date(),"yyyyMMddHHmmssSSS")+"."+fileExt;
	                  String picUrl=url+file_name;
	                  Map<String, Object> photosMap1 = new HashMap<String, Object>();
	                  photosMap1.put("url", picUrl);
	      			  photos.add(photosMap1);
	                  File savedFile = new File(path, file_name);
	                  fi.write(savedFile);
	              }
	           }
	           System.out.print("upload succeed");
	           map.put("id", id);
	           map.put("arrs", photos);
	           JSONObject json = new JSONObject(map);
	           WebUtil.response(resp,json);
	       } catch (Exception e) {
	           // 可以跳转出错页面
	           e.printStackTrace();
	       }
		
	}

	
}
